

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>saml2.logout_request &mdash; SAML Python library classes and methods</title>

    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />

    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title=" SAML Python library classes and methods" href="../../index.html" />
    <link rel="up" title="Class code" href="../index.html" />
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Class Index"
             >modules</a> |</li>
        <li><a href="../../index.html">SAML Python library classes and methods</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Class code</a> &raquo;</li>
      </ul>
    </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">

  <h1>Source code for saml2.logout_request</h1><div class="highlight"><pre>
<span class="c"># -*- coding: utf-8 -*-</span>

<span class="kn">from</span> <span class="nn">base64</span> <span class="kn">import</span> <span class="n">b64decode</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">from</span> <span class="nn">lxml</span> <span class="kn">import</span> <span class="n">etree</span>
<span class="kn">from</span> <span class="nn">os.path</span> <span class="kn">import</span> <span class="n">basename</span>
<span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">urlencode</span>
<span class="kn">from</span> <span class="nn">urlparse</span> <span class="kn">import</span> <span class="n">parse_qs</span>
<span class="kn">from</span> <span class="nn">xml.dom.minidom</span> <span class="kn">import</span> <span class="n">Document</span><span class="p">,</span> <span class="n">parseString</span>

<span class="kn">import</span> <span class="nn">dm.xmlsec.binding</span> <span class="kn">as</span> <span class="nn">xmlsec</span>

<span class="kn">from</span> <span class="nn">saml2.constants</span> <span class="kn">import</span> <span class="n">OneLogin_Saml2_Constants</span>
<span class="kn">from</span> <span class="nn">saml2.utils</span> <span class="kn">import</span> <span class="n">OneLogin_Saml2_Utils</span>


<div class="viewcode-block" id="OneLogin_Saml2_Logout_Request"><a class="viewcode-back" href="../../saml2.html#saml2.logout_request.OneLogin_Saml2_Logout_Request">[docs]</a><span class="k">class</span> <span class="nc">OneLogin_Saml2_Logout_Request</span><span class="p">:</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">settings</span>,<span class="n">request</span><span class="o">=</span><span class="bp">None</span><span class="p">,<span class="n">name_id</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">session_index</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Constructs the Logout Request object.</span>

<span class="sd">        Arguments are:</span>
<span class="sd">            * (OneLogin_Saml2_Settings)   settings. Setting data</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span> <span class="o">=</span> <span class="n">settings</span>

        <span class="n">sp_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span><span class="o">.</span><span class="n">get_sp_data</span><span class="p">()</span>
        <span class="n">idp_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span><span class="o">.</span><span class="n">get_idp_data</span><span class="p">()</span>
        <span class="n">security</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span><span class="o">.</span><span class="n">get_security_data</span><span class="p">()</span>

        <span class="n">uid</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">generate_unique_id</span><span class="p">()</span>
        <span class="n">name_id_value</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">generate_unique_id</span><span class="p">()</span>
        <span class="n">issue_instant</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">parse_time_to_SAML</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">&quot;</span><span class="p">)))</span>

        <span class="n">key</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="k">if</span> <span class="s">&#39;nameIdEncrypted&#39;</span> <span class="ow">in</span> <span class="n">security</span> <span class="ow">and</span> <span class="n">security</span><span class="p">[</span><span class="s">&#39;nameIdEncrypted&#39;</span><span class="p">]:</span>
            <span class="n">key</span> <span class="o">=</span> <span class="n">idp_data</span><span class="p">[</span><span class="s">&#39;x509cert&#39;</span><span class="p">]</span>

        <span class="n">name_id</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">generate_name_id</span><span class="p">(</span>
            <span class="n">name_id_value</span><span class="p">,</span>
            <span class="n">sp_data</span><span class="p">[</span><span class="s">&#39;entityId&#39;</span><span class="p">],</span>
            <span class="n">sp_data</span><span class="p">[</span><span class="s">&#39;NameIDFormat&#39;</span><span class="p">],</span>
            <span class="n">key</span>
        <span class="p">)</span>

        <span class="n">logout_request</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;&lt;samlp:LogoutRequest</span>
<span class="s">    xmlns:samlp=&quot;urn:oasis:names:tc:SAML:2.0:protocol&quot;</span>
<span class="s">    xmlns:saml=&quot;urn:oasis:names:tc:SAML:2.0:assertion&quot;</span>
<span class="s">    ID=&quot;</span><span class="si">%(id)s</span><span class="s">&quot;</span>
<span class="s">    Version=&quot;2.0&quot;</span>
<span class="s">    IssueInstant=&quot;</span><span class="si">%(issue_instant)s</span><span class="s">&quot;</span>
<span class="s">    Destination=&quot;</span><span class="si">%(single_logout_url)s</span><span class="s">&quot;&gt;</span>
<span class="s">    &lt;saml:Issuer&gt;</span><span class="si">%(entity_id)s</span><span class="s">&lt;/saml:Issuer&gt;</span>
<span class="s">    </span><span class="si">%(name_id)s</span><span class="s"></span>
<span class="s">&lt;/samlp:LogoutRequest&gt;&quot;&quot;&quot;</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&#39;id&#39;</span><span class="p">:</span> <span class="n">uid</span><span class="p">,</span>
            <span class="s">&#39;issue_instant&#39;</span><span class="p">:</span> <span class="n">issue_instant</span><span class="p">,</span>
            <span class="s">&#39;single_logout_url&#39;</span><span class="p">:</span> <span class="n">idp_data</span><span class="p">[</span><span class="s">&#39;singleLogoutService&#39;</span><span class="p">][</span><span class="s">&#39;url&#39;</span><span class="p">],</span>
            <span class="s">&#39;entity_id&#39;</span><span class="p">:</span> <span class="n">sp_data</span><span class="p">[</span><span class="s">&#39;entityId&#39;</span><span class="p">],</span>
            <span class="s">&#39;name_id&#39;</span><span class="p">:</span> <span class="n">name_id</span><span class="p">,</span>
        <span class="p">}</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">__logout_request</span> <span class="o">=</span> <span class="n">logout_request</span>

<div class="viewcode-block" id="OneLogin_Saml2_Logout_Request.get_request"><a class="viewcode-back" href="../../saml2.html#saml2.logout_request.OneLogin_Saml2_Logout_Request.get_request">[docs]</a>    <span class="k">def</span> <span class="nf">get_request</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the Logout Request defated, base64encoded</span>
<span class="sd">        :return: Deflated base64 encoded Logout Request</span>
<span class="sd">        :rtype: str object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">deflate_and_base64_encode</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__logout_request</span><span class="p">)</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Logout_Request.get_id"><a class="viewcode-back" href="../../saml2.html#saml2.logout_request.OneLogin_Saml2_Logout_Request.get_id">[docs]</a>    <span class="k">def</span> <span class="nf">get_id</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the ID of the Logout Request</span>
<span class="sd">        :param request: Logout Request Message</span>
<span class="sd">        :type request: string|DOMDocument</span>
<span class="sd">        :return: string ID</span>
<span class="sd">        :rtype: str object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">Document</span><span class="p">):</span>
            <span class="n">dom</span> <span class="o">=</span> <span class="n">request</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">dom</span> <span class="o">=</span> <span class="n">parseString</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">dom</span><span class="o">.</span><span class="n">documentElement</span><span class="o">.</span><span class="n">getAttribute</span><span class="p">(</span><span class="s">&#39;ID&#39;</span><span class="p">)</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Logout_Request.get_name_id_data"><a class="viewcode-back" href="../../saml2.html#saml2.logout_request.OneLogin_Saml2_Logout_Request.get_name_id_data">[docs]</a>    <span class="k">def</span> <span class="nf">get_name_id_data</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the NameID Data of the the Logout Request</span>
<span class="sd">        :param request: Logout Request Message</span>
<span class="sd">        :type request: string|DOMDocument</span>
<span class="sd">        :param key: The SP key</span>
<span class="sd">        :type key: string</span>
<span class="sd">        :return: Name ID Data (Value, Format, NameQualifier, SPNameQualifier)</span>
<span class="sd">        :rtype: dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">Document</span><span class="p">):</span>
            <span class="n">request</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">toxml</span><span class="p">()</span>
        <span class="n">dom</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="n">name_id</span> <span class="o">=</span> <span class="bp">None</span>

        <span class="n">encrypted_entries</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">dom</span><span class="p">,</span> <span class="s">&#39;/samlp:LogoutRequest/saml:EncryptedID&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">encrypted_entries</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Key is required in order to decrypt the NameID&#39;</span><span class="p">)</span>

            <span class="n">elem</span> <span class="o">=</span> <span class="n">parseString</span><span class="p">(</span><span class="n">etree</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">encrypted_entries</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
            <span class="n">encrypted_data_nodes</span> <span class="o">=</span> <span class="n">elem</span><span class="o">.</span><span class="n">documentElement</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span><span class="s">&#39;xenc:EncryptedData&#39;</span><span class="p">)</span>
            <span class="n">encrypted_data</span> <span class="o">=</span> <span class="n">encrypted_data_nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

            <span class="n">xmlsec</span><span class="o">.</span><span class="n">initialize</span><span class="p">()</span>

            <span class="c"># Load the key into the xmlsec context</span>
            <span class="n">file_key</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">write_temp_file</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>  <span class="c"># FIXME avoid writing a file</span>
            <span class="n">enc_key</span> <span class="o">=</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">Key</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">file_key</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">KeyDataFormatPem</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
            <span class="n">enc_key</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">basename</span><span class="p">(</span><span class="n">file_key</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">file_key</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
            <span class="n">enc_ctx</span> <span class="o">=</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">EncCtx</span><span class="p">()</span>
            <span class="n">enc_ctx</span><span class="o">.</span><span class="n">encKey</span> <span class="o">=</span> <span class="n">enc_key</span>

            <span class="n">name_id</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">decrypt_element</span><span class="p">(</span><span class="n">encrypted_data</span><span class="p">,</span> <span class="n">enc_ctx</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">entries</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">dom</span><span class="p">,</span> <span class="s">&#39;/samlp:LogoutRequest/saml:NameID&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">entries</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">name_id</span> <span class="o">=</span> <span class="n">entries</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">name_id</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Not NameID found in the Logout Request&#39;</span><span class="p">)</span>

        <span class="n">name_id_data</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">&#39;Value&#39;</span><span class="p">:</span> <span class="n">name_id</span><span class="o">.</span><span class="n">text</span>
        <span class="p">}</span>
        <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;Format&#39;</span><span class="p">,</span> <span class="s">&#39;SPNameQualifier&#39;</span><span class="p">,</span> <span class="s">&#39;NameQualifier&#39;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">name_id</span><span class="o">.</span><span class="n">attrib</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="n">name_id_data</span><span class="p">[</span><span class="n">attr</span><span class="p">]</span> <span class="o">=</span> <span class="n">name_id</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="n">attr</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">name_id_data</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Logout_Request.get_name_id"><a class="viewcode-back" href="../../saml2.html#saml2.logout_request.OneLogin_Saml2_Logout_Request.get_name_id">[docs]</a>    <span class="k">def</span> <span class="nf">get_name_id</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the NameID of the Logout Request Message</span>
<span class="sd">        :param request: Logout Request Message</span>
<span class="sd">        :type request: string|DOMDocument</span>
<span class="sd">        :param key: The SP key</span>
<span class="sd">        :type key: string</span>
<span class="sd">        :return: Name ID Value</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">name_id</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Logout_Request</span><span class="o">.</span><span class="n">get_name_id_data</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">name_id</span><span class="p">[</span><span class="s">&#39;Value&#39;</span><span class="p">]</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Logout_Request.get_issuer"><a class="viewcode-back" href="../../saml2.html#saml2.logout_request.OneLogin_Saml2_Logout_Request.get_issuer">[docs]</a>    <span class="k">def</span> <span class="nf">get_issuer</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the Issuer of the Logout Request Message</span>
<span class="sd">        :param request: Logout Request Message</span>
<span class="sd">        :type request: string|DOMDocument</span>
<span class="sd">        :return: The Issuer</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">Document</span><span class="p">):</span>
            <span class="n">request</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">toxml</span><span class="p">()</span>
        <span class="n">dom</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

        <span class="n">issuer</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="n">issuer_nodes</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">dom</span><span class="p">,</span> <span class="s">&#39;/samlp:LogoutRequest/saml:Issuer&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">issuer_nodes</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">issuer</span> <span class="o">=</span> <span class="n">issuer_nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">text</span>
        <span class="k">return</span> <span class="n">issuer</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Logout_Request.get_session_indexes"><a class="viewcode-back" href="../../saml2.html#saml2.logout_request.OneLogin_Saml2_Logout_Request.get_session_indexes">[docs]</a>    <span class="k">def</span> <span class="nf">get_session_indexes</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the SessionIndexes from the Logout Request</span>
<span class="sd">        :param request: Logout Request Message</span>
<span class="sd">        :type request: string|DOMDocument</span>
<span class="sd">        :return: The SessionIndex value</span>
<span class="sd">        :rtype: list</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">Document</span><span class="p">):</span>
            <span class="n">request</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">toxml</span><span class="p">()</span>
        <span class="n">dom</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

        <span class="n">session_indexes</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">session_index_nodes</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">dom</span><span class="p">,</span> <span class="s">&#39;/samlp:LogoutRequest/samlp:SessionIndex&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">session_index_node</span> <span class="ow">in</span> <span class="n">session_index_nodes</span><span class="p">:</span>
            <span class="n">session_indexes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">session_index_node</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">session_indexes</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Logout_Request.is_valid"><a class="viewcode-back" href="../../saml2.html#saml2.logout_request.OneLogin_Saml2_Logout_Request.is_valid">[docs]</a>    <span class="k">def</span> <span class="nf">is_valid</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">get_data</span><span class="p">,</span> <span class="n">debug</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Checks if the Logout Request recieved is valid</span>
<span class="sd">        :param settings: Settings</span>
<span class="sd">        :type settings: OneLogin_Saml2_Settings</span>
<span class="sd">        :param request: Logout Request Message</span>
<span class="sd">        :type request: string|DOMDocument</span>
<span class="sd">        :return: If the Logout Request is or not valid</span>
<span class="sd">        :rtype: boolean</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">Document</span><span class="p">):</span>
                <span class="n">dom</span> <span class="o">=</span> <span class="n">request</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">dom</span> <span class="o">=</span> <span class="n">parseString</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>

            <span class="n">idp_data</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">get_idp_data</span><span class="p">()</span>
            <span class="n">idp_entity_id</span> <span class="o">=</span> <span class="n">idp_data</span><span class="p">[</span><span class="s">&#39;entityId&#39;</span><span class="p">]</span>

            <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">is_strict</span><span class="p">():</span>
                <span class="n">res</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">validate_xml</span><span class="p">(</span><span class="n">dom</span><span class="p">,</span> <span class="s">&#39;saml-schema-protocol-2.0.xsd&#39;</span><span class="p">,</span> <span class="n">debug</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="n">Document</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Invalid SAML Logout Request. Not match the saml-schema-protocol-2.0.xsd&#39;</span><span class="p">)</span>

                <span class="n">security</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">get_security_data</span><span class="p">()</span>

                <span class="n">current_url</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">get_self_url_no_query</span><span class="p">(</span><span class="n">get_data</span><span class="p">)</span>

                <span class="c"># Check NotOnOrAfter</span>
                <span class="k">if</span> <span class="n">dom</span><span class="o">.</span><span class="n">documentElement</span><span class="o">.</span><span class="n">hasAttribute</span><span class="p">(</span><span class="s">&#39;NotOnOrAfter&#39;</span><span class="p">):</span>
                    <span class="n">na</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">parse_SAML_to_time</span><span class="p">(</span><span class="n">dom</span><span class="o">.</span><span class="n">documentElement</span><span class="o">.</span><span class="n">getAttribute</span><span class="p">(</span><span class="s">&#39;NotOnOrAfter&#39;</span><span class="p">))</span>
                    <span class="k">if</span> <span class="n">na</span> <span class="o">&lt;=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">():</span>
                        <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Timing issues (please check your clock settings)&#39;</span><span class="p">)</span>

                <span class="c"># Check destination</span>
                <span class="k">if</span> <span class="n">dom</span><span class="o">.</span><span class="n">documentElement</span><span class="o">.</span><span class="n">hasAttribute</span><span class="p">(</span><span class="s">&#39;Destination&#39;</span><span class="p">):</span>
                    <span class="n">destination</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">documentElement</span><span class="o">.</span><span class="n">getAttribute</span><span class="p">(</span><span class="s">&#39;Destination&#39;</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">destination</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">current_url</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">destination</span><span class="p">:</span>
                            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;The LogoutRequest was received at $currentURL instead of $destination&#39;</span><span class="p">)</span>

                <span class="c"># Check issuer</span>
                <span class="n">issuer</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Logout_Request</span><span class="o">.</span><span class="n">get_issuer</span><span class="p">(</span><span class="n">dom</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">issuer</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="n">issuer</span> <span class="o">!=</span> <span class="n">idp_entity_id</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Invalid issuer in the Logout Request&#39;</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">security</span><span class="p">[</span><span class="s">&#39;wantMessagesSigned&#39;</span><span class="p">]:</span>
                    <span class="k">if</span> <span class="s">&#39;Signature&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">get_data</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;The Message of the Logout Request is not signed and the SP require it&#39;</span><span class="p">)</span>

            <span class="k">if</span> <span class="s">&#39;Signature&#39;</span> <span class="ow">in</span> <span class="n">get_data</span><span class="p">:</span>
                <span class="k">if</span> <span class="s">&#39;SigAlg&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">get_data</span><span class="p">:</span>
                    <span class="n">sign_alg</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Constants</span><span class="o">.</span><span class="n">RSA_SHA1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">sign_alg</span> <span class="o">=</span> <span class="n">get_data</span><span class="p">[</span><span class="s">&#39;SigAlg&#39;</span><span class="p">]</span>

                <span class="k">if</span> <span class="n">sign_alg</span> <span class="o">!=</span> <span class="n">OneLogin_Saml2_Constants</span><span class="o">.</span><span class="n">RSA_SHA1</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Invalid signAlg in the recieved Logout Request&#39;</span><span class="p">)</span>

                <span class="n">signed_query</span> <span class="o">=</span> <span class="s">&#39;SAMLRequest=</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">urlencode</span><span class="p">(</span><span class="n">get_data</span><span class="p">[</span><span class="s">&#39;SAMLRequest&#39;</span><span class="p">])</span>
                <span class="k">if</span> <span class="s">&#39;RelayState&#39;</span> <span class="ow">in</span> <span class="n">get_data</span><span class="p">:</span>
                    <span class="n">signed_query</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">&amp;RelayState=</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">signed_query</span><span class="p">,</span> <span class="n">urlencode</span><span class="p">(</span><span class="n">get_data</span><span class="p">[</span><span class="s">&#39;RelayState&#39;</span><span class="p">]))</span>
                <span class="n">signed_query</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">&amp;SigAlg=</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">signed_query</span><span class="p">,</span> <span class="n">urlencode</span><span class="p">(</span><span class="n">sign_alg</span><span class="p">))</span>

                <span class="k">if</span> <span class="s">&#39;x509cert&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">idp_data</span> <span class="ow">or</span> <span class="n">idp_data</span><span class="p">[</span><span class="s">&#39;x509cert&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;In order to validate the sign on the Logout Request, the x509cert of the IdP is required&#39;</span><span class="p">)</span>
                <span class="n">cert</span> <span class="o">=</span> <span class="n">idp_data</span><span class="p">[</span><span class="s">&#39;x509cert&#39;</span><span class="p">]</span>

                <span class="n">xmlsec</span><span class="o">.</span><span class="n">initialize</span><span class="p">()</span>
                <span class="n">objkey</span> <span class="o">=</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">Key</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">cert</span><span class="p">,</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">KeyDataFormatPem</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>  <span class="c"># FIXME is this right?</span>

                <span class="k">if</span> <span class="ow">not</span> <span class="n">objkey</span><span class="o">.</span><span class="n">verifySignature</span><span class="p">(</span><span class="n">signed_query</span><span class="p">,</span> <span class="n">b64decode</span><span class="p">(</span><span class="n">get_data</span><span class="p">[</span><span class="s">&#39;Signature&#39;</span><span class="p">])):</span>
                    <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Signature validation failed. Logout Request rejected&#39;</span><span class="p">)</span>

            <span class="k">return</span> <span class="bp">True</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">debug</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">is_debug_active</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">debug</span><span class="p">:</span>
                <span class="k">print</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">strerror</span><span class="p">)</span>
            <span class="k">return</span> <span class="bp">False</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Class Index"
             >modules</a> |</li>
        <li><a href="../../index.html">SAML Python library classes and methods</a> &raquo;</li>
          <li><a href="../index.html" >Class code</a> &raquo;</li>
      </ul>
    </div>
    <div class="footer">
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>
